package com.java.lambda;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Lambda {
    private String name;
    
    public Lambda(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
        return name;
    }
    
    public static void main(String[] args) {
        List<Lambda> list = new ArrayList();
        list.add(new Lambda("MJQ"));
        list.add(new Lambda("DCX"));
        list.add(new Lambda("SYX"));
        list.add(new Lambda("LYW"));
        list.add(new Lambda("ZZY"));
        list.add(new Lambda("YHX"));
        list.add(new Lambda("HJL"));
        
        // 匿名内部类
        Collections.sort(list, new Comparator<Lambda>() {
            @Override
            public int compare(Lambda o1, Lambda o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        
        // lambda表达式：参数类型可省略、只有一个参数()可省略、方法体只有一句{};return可省略
        Collections.sort(list, (o1, o2) -> o1.name.compareTo(o2.name));
        
        System.out.println(list); // [DCX, HJL, LYW, MJQ, SYX, YHX, ZZY]
    }
}
